package com.ershi.spring.aop.proxy.interceptor;


import com.ershi.spring.aop.proxy.common.TargetMethod;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 二十
 * @since 2021/11/1 12:04 上午
 */
public interface MyInvocation {

	Object proceed();
}
class MyInvocationImpl implements MyInvocation{

	List<MyInterceptor> interceptors = new ArrayList<>();

	int size =0;

	TargetMethod targetMethod;


	@Override
	public Object proceed() {

		try {
			return size == interceptors.size()?
					targetMethod.getMethod()
							.invoke(
									targetMethod.getTarget(),
									targetMethod.getArgs()
							):
					interceptors.get(size++)
							.invoke(this);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	public MyInvocationImpl(List<MyInterceptor> interceptors, TargetMethod targetMethod) {
		this.interceptors = interceptors;
		this.targetMethod = targetMethod;
	}
}
